#include "recboxitem.h"
#include "ui_recboxitem.h"
#include <QDebug>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);

    //添加事件过滤器
    ui->recMusicImage->installEventFilter(this);

}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

void RecBoxItem::setBoxItemText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setRecImage(const QString &path)
{
    QString style = "background-image:url("+path+");";
    ui->recMusicImage->setStyleSheet(style);
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->recMusicImage)
    {
        if(QEvent::Enter == event->type())
        {

            //图片上升动画
            QPropertyAnimation *animation = new QPropertyAnimation(ui->musicImageBox, "geometry", this);
            animation->setDuration(150);
            animation->setStartValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            //动画完成后释放对象
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });

            return true;
        }
        else if(QEvent::Leave == event->type())
        {
            //图片下升动画
            QPropertyAnimation *animation = new QPropertyAnimation(ui->musicImageBox, "geometry", this);
            animation->setDuration(150);
            animation->setStartValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            //动画完成后释放对象
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });

            return true;
        }
    }

    return QObject::eventFilter(watched, event);
}
